Random Number Simulations

© 2005 by Welopez

author contact:

welopez@starband.net

Home

Tip Corner

Youth Corner

Stylebits Corner

Custom Cursors

LB Wire Frame Lib

Random Numbers

Eddie Version 5

Page Orientation

Help

Index

Computers are frequently utilized to simulate real life events. Sometimes the simulations can accurately predict whether a business will succeed or fail, based upon the algorithim used for success or failure.

Many games depend upon random numbers to simulate the decision making process in real life. Basic includes the RND(0) function to generate pseudo-random numbers, that is, decisions can be made within fairly predictable ranges. The value of a random number generator depends upon the unpredictability of the next occurrence of an event. The probability of an event occuring can be calculated. Suppose you are flipping a coin, there can only be two outcomes, heads or tails. (We assume that you are not fumble fingered and will drop the coin into a storm drain on the next toss, but we can approximate that condition also.)

If we toss the coin 1,000 times, we would calculate the outcome to be heads = 500, tails = 500. In real life, with a real coin, we might observe a frequency of 490 heads versus 510 tails. There is nothing "un-real" about this real-life event. In this example, P = O / E . The actual Probality equals the Observed frequency divided by the Expected frequency . P is something we deal with in everyday events. (There is no such thing as The Law of Averages, that bill died in committee and was never passed.)

num=INT(RND(0)*10)+1

RND(0) returns a value between zero and one everytime. So does RND(1), RND(2), etc. The value within parenthesis makes no difference. We want num to be between 1 and 10, so we multiply RND(0) by 10. The value returned from the function can be extremely small, .000345, for example. 10 * .000345 = .00345, and will not produce a number we want, so we add 1 to the value, remove the decimal portion with the INT function, and our first number chosen is 1. If you don't understand this simple procedure, go back and read it again, for it is critical that you do.

If the value returned by RND(0) is .876543, then multiplying by 10 and taking the INT value produces 8. Now we have a number we can work with. Copy and paste this code and run it for a various number of simulations.

'Working with RANDOM

'RANDOMIZE .5
DIM numb(10)

INPUT "How many simulations? "; sim
begin=TIME$("ms")
FOR i=1 TO sim
    j=INT(RND(0)*10)+1
    numb(j)=numb(j)+1
NEXT i
finish=TIME$("ms")

PRINT "NUMBER"; TAB(10); "FREQUENCY"; TAB(30); "PROBABILITY"
FOR i=1 TO 10
    PRINT i; TAB(10); numb(i); TAB(30); (numb(i)/(sim/10))
NEXT i
PRINT
PRINT "The simulation of "; sim; " was finished in "; finish-begin ; " milliseconds."

END

Notice that RANDOMIZE .5 has been REMmed out. We won't use it at the moment, but we'll discuss it later.

If you run for 1,000 simulations, you would expect 1 to occur 100 times, 100 times for 2, 100 times for 3, and so on. On occasion, P will = 1, but rarely. Real life is not 100%predictable either. Run the program for a million simulations. You'll get reasonably close, but never precisely obtain the results you expect. I've bossed craps tables in legal casinos for 15 years. I can assure you, the probability of two honest dice rolling a two or a twelve are 1 in 36. Therefore, the probability of the next roll being either a two or twelve is precisely 2/36, or one in eighteen chances. The probability of rolling either a two or twelve in five consecutive rolls is 18^5, or one in 1,889,568 rolls of the dice. Yet I actually watched this happen on one occasion. Does this mean someone switched my dice? No, it simply means I have stood around a craps table for a very long time, the probability of large numbers caught up to me.

Okay, I promised we would look at the RANDOMIZE n statement. N must be a value between 0 and 1, any value you choose will be satisfactory. RANDOMIZE n will produce the identical sequence of random values every time the program is run. Of course, you will obtain a different sequence for each value of n selected. Why would anyone want to repeat the same sequence? It can be useful in scientific or engineering applications where the observer wants to compare a given output when modifications are made to a device or process elsewhere in the simulation. For gaming purposes, you will rarely want to use the RANDOMIZE statement.

Let's toss that imaginary coin a few thousand times. In this simulation, a ONE returned by the random number generator will be HEADS. TWO will be TAILS. result(coin) will be an accumulator to count how many times heads or tails occurred.

'Flip a Coin

INPUT "How many times to flip the coin? "; flip
'HEADS = coin(1), TAILS = coin(2)

begin=TIME$("ms")
FOR i= 1 TO flip
    coin=INT(RND(0)*2)+1
    result(coin)=result(coin)+1
NEXT i
finish=TIME$("ms")
PRINT
PRINT "There were "; result(1); " heads."
PRINT "There were "; result(2); " tails."
PRINT
PRINT "Simulation of "; flip; " coin flips complete in "; finish-begin; " milliseconds."

END

I use TIME$() to get the begin and finish time just for fun. You can delete that if you like. Run the code for a few thousand tosses and you will get different results most times. P will be close to what you expect, but never exact.

Does this mean computer simulations are worthless for the real world? No, they will remain useful, but you must remember the values produced are only approximations. Laboratories and businesses which depend upon extremely random results (your state lottery, or a legal casino, perhaps?), will use a complicated algorithm, or even sophisticated hardware to generate "white noise" or some other unpredictable value.

Everyone wants to find an "undiscovered" method to avoid losing at the dice table, so I'll give you a 100% sure-fire system, and it won't cost you a dime. When you get to Las Vegas, take $100, go to the best restaurant you can find and order a fantastic meal. Take whatever remains and go to see a good show, have a few drinks, and remember to leave a tip for the waitress. Do not go to the dice table, roulette wheel, or 21 table! In my professional experience, not losing equates with winning, especially if you have had a good meal and enjoyed watching the pretty dancing girls!

Okay, how do you simulate rolling dice?

'Two DICE

DIM dice(12)
INPUT "How many times will we roll the dice? "; rolls

begin=TIME$("ms")
FOR i=1 TO rolls
    die1=INT(RND(0)*6)+1
    die2=INT(RND(0)*6)+1
    numRolled=die1+die2
    dice(numRolled)=dice(numRolled)+1
NEXT i
finish=TIME$("ms")

PRINT
PRINT "Num Rolled"; TAB(15); "FREQUENCY"
FOR i=2 TO 12
    PRINT i, dice(i)
NEXT i
PRINT
PRINT "The dice were rolled "; rolls; " times in "; finish-begin; " milliseconds."

END

The code is the same as for generating any other random value. With dice, however, it is important to understand the possible numbers which can be rolled are the sum of two smaller numbers. Each die (singular of dice) can have one of six values. The "point" is the sum of two dice. Got that?

The odds on a dice table depend upon the possible combinations of two dice. Two can only be rolled one way, if each die comes up on 1. Twelve can only be rolled one way, and each die must come up on 6. The probailities of any number, 2 through 12, are shown like this:

--Number-----Odds-----Combos--
2 1/36 1+1
3 2/36 1+2, 2+1
4 3/36 1+3, 3+1, 2+2 (the hard way!)
5 4/36 1+4, 4+1, 2+3, 3+2
6 5/36 1+5, 5+1, 2+4, 4+2, 3+3
7 6/36 1+6, 6+1, 2+5, 5+2, 3+4, 4+3
8 5/36 2+6, 6+2, 3+5, 5+3, 4+4
9 4/36 3+6, 6+3, 4+5, 5+4
10 3/36 4+6, 6+4, 5+5
11 2/36 5+6, 6+5
12 1/36 6+6

Now that you know all the probabilities, feel free to code your own simulation. When you have a winning system, bring all the cash you can lay your hands on to Las Vegas and take a chance. I guarantee you will lose, but you'll be a winner in my books because you are helping to keep my taxes low!

When using random numbers to make decisions in a computer game, often you will want to weight the results of a decision. Let's say Joe Worble has arrived at a crossroad. We can use the same simulation as flipping a coin to decide if Joe will go left or right. But if Joe hesitates while trying to make up his mind, there is always the possibility he may be hit by a falling meteor and die!

'Weighting a decision using RND(0)

INPUT "How many decisions will be made? "; dec

begin=TIME$("ms")
FOR i=1 TO dec
    choice=INT(RND(0)*100)+1
    IF choice >= 90 THEN dead=dead+1
NEXT i
finish=TIME$("ms")
PRINT
PRINT dec; " decisions were made in "; finish-begin; " milliseconds."
PRINT "You hesitated and died "; dead; " times."

END

In this code, if the random value returned is equal or greater than 90, Joe will die. That's a pretty steep probability of being struck by a meteor, but you get the idea. Your code might be <=10, >44 OR <55, the probability will be the same, the chances of Joe being fitted for a halo will be 1 in 10.

Now you should have a bang-up idea how to use the RND(0) function when programming in Basic. Remember you can use any of the several types of looping routines available in your Basic tool box. Don't forget to zero out your accumulators if you run the program again without closing, or you may find P equals 1000 by mistake. That should be your first tip-off that you've made an error.


Home

Tip Corner

Youth Corner

Stylebits Corner

Custom Cursors

LB Wire Frame Lib

Random Numbers

Eddie Version 5

Page Orientation

Help

Index